var ljll = {
    dataOptions: {
        pageNumber:1,
        showFooter: true,
        onDblClickCell: function (index, field) {

        },
        onLoadSuccess: function () {
            $(this).datagrid('selectRow', 0);
        },
        loadFilter: function (data) {
            return data;
        },
        onClickRow: function () {

        },
        onSelect: function (rowIndex, rowData) {

        },
        rowStyler: function (index, row) {
        },
        columns: [
            [
                {field: 'cgd', title: '采购单', align: 'left', width: 100},
                {field: 'cgdje', title: '采购单金额', align: 'left', width: 40},
                {field: 'xsd', title: '销售单', align: 'left', width: 100},
                {field: 'xsdje', title: '销售单金额', align: 'left', width: 40},
                {field: 'sjlr', title: '实际利润', align: 'left', width: 40},
                {field: 'sjll', title: '实际利率', align: 'left', width: 20},
                {field: 'ljll', title: '路径利率', align: 'left', width: 20}
            ]
        ]
    },
    //初始化表数据
    initDatagrid: function () {
        $('#grid_ljll').datagrid(ljll.dataOptions);
    },
    initBtn: function () {
        //查询
        $("#btn_ljll_search").click(function () {
            var ljid = $('#query_lj').combobox('getValue');
            var timeFrom = $('#timeFrom').datebox('getValue');
            var timeTo = $('#timeTo').datebox('getValue');
            if (ljid) {
                $('#grid_ljll').datagrid({url: $.webPath() + "/tjbb/ljsjll/ljllList", queryParams: {ljid: ljid, timeFrom: timeFrom, timeTo: timeTo}});
            }
        });
    },
    initQuery: function () {  //公司机构下拉
        $("#query_lj").combobox({
            panelHeight: '200',
            valueField: 'id',
            textField: 'mc'
        });

        var option = {
            url: $.webPath() + '/organ/gsjg/gsjg_tree?type=1&gsjgId=' + $("#lj_zggsId").val(),
            panelHeight: '200',
            loadFilter: function (rows) {
                return convert(rows);
            },
            onLoadSuccess: function () {
                $("#query_lj_jyjg").val(1);
                $("#query_lj_jyjg").combotree("setValue", $(this).tree("getRoot").id);
            },
            onChange: function (newValue, oldValue) {
                $('#query_lj').combobox('clear');
                $('#query_lj').combobox('reload', $.webPath() + '/organ/ljwh/ljinfo?queryLj.jyjg=' + newValue + "&queryLj.lx=0");
            }
        }

        $("#query_lj_jyjg").combotree(option);
    },
    initDatebox: function () {
        $('#timeFrom').datebox({
            onSelect: function (date) {
                var toDate = $.fn.datebox.defaults.parser($('#timeTo').datebox('getValue'));
                var timeTo = toDate.getFullYear() + "-" + ("00" + (toDate.getMonth() + 1)).substr(-2, 2) + "-" + ("00" + (toDate.getDate())).substr(-2, 2);
                if (date > toDate) {  //开始时间大于结束时间
                    toDate = date;
                    timeTo = toDate.getFullYear() + "-" + ("00" + (toDate.getMonth() + 1)).substr(-2, 2) + "-" + ("00" + (toDate.getDate())).substr(-2, 2);
                }
                else {
                    var fromMonth = date.getFullYear() * 12 + date.getMonth(); //开始月数
                    var toMonth = toDate.getFullYear() * 12 + toDate.getMonth(); //结束月数
                    if (fromMonth < toMonth - 3 ||
                        (fromMonth == toMonth - 3 && date.getDate() < toDate.getDate())
                        ) {   //时间间隔大于３个月
                        toMonth = fromMonth + 3;
                        timeTo = parseInt(toMonth / 12) + "-" + ("00" + (toMonth % 12 + 1)).substr(-2, 2) + "-" + ("00" + (date.getDate())).substr(-2, 2);
                    }
                }
                $('#timeTo').datebox('setValue', timeTo);
            }
        });
        $('#timeTo').datebox({
            onSelect: function (date) {
                var fromDate = $.fn.datebox.defaults.parser($('#timeFrom').datebox('getValue'));
                var timeFrom = fromDate.getFullYear() + "-" + ("00" + (fromDate.getMonth() + 1)).substr(-2, 2) + "-" + ("00" + (fromDate.getDate())).substr(-2, 2);
                if (fromDate > date) {  //开始时间大于结束时间
                    fromDate = date;
                    timeFrom = fromDate.getFullYear() + "-" + ("00" + (fromDate.getMonth() + 1)).substr(-2, 2) + "-" + ("00" + (fromDate.getDate())).substr(-2, 2);
                }
                else {
                    var fromMonth = fromDate.getFullYear() * 12 + fromDate.getMonth(); //开始月数
                    var toMonth = date.getFullYear() * 12 + date.getMonth(); //结束月数
                    if (fromMonth < toMonth - 3 ||
                        (fromMonth == toMonth - 3 && fromDate.getDate() < date.getDate())
                        ) {   //时间间隔大于３个月
                        fromMonth = toMonth - 3;
                        timeFrom = parseInt(fromMonth / 12) + "-" + ("00" + (fromMonth % 12 + 1)).substr(-2, 2) + "-" + ("00" + (date.getDate())).substr(-2, 2);
                    }
                }
                $('#timeFrom').datebox('setValue', timeFrom);
            }
        });

        //默认开始时间
        var curTime = new Date();
        $('#timeFrom').datebox('setValue', curTime.getFullYear() + "-" + ("00" + (curTime.getMonth() + 1)).substr(-2, 2) + "-" + ("00" + (curTime.getDate())).substr(-2, 2));
    }
}

$(function () {
    var winHeight = $(window).height();
    $("#mainFrame").css('height', (winHeight - 120) + "px");
    //列表-加载
    ljll.initDatagrid();
    //按钮事件-加载
    ljll.initBtn();
    //查询条件-加载
    ljll.initQuery();
    //时间
    ljll.initDatebox();
});

function myformatter(date) {
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    var d = date.getDate();
    return y + '-' + (m < 10 ? ('0' + m) : m) + '-' + (d < 10 ? ('0' + d) : d);
}
function myparser(s) {
    if (!s) return new Date();
    var ss = s.split('-');
    var y = parseInt(ss[0], 10);
    var m = parseInt(ss[1], 10);
    var d = parseInt(ss[2], 10);
    if (!isNaN(y) && !isNaN(m) && !isNaN(d)) {
        return new Date(y, m - 1, d);
    } else {
        return new Date();
    }
}

